package com.qfedu.controller;

import com.alibaba.fastjson.JSONObject;
import com.qfedu.entity.Admin;
import com.qfedu.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author zhang
 * @Title: commonValidateAdd
 * @date : 2022/2/24 23:07
 */
@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping("/admin/login/status")
    @ResponseBody
    public JSONObject login(String name, String password, HttpSession httpSession){
        //根据 username 找到对应的用户
        List<Admin> login = loginService.login(name, password);
        JSONObject jsonObject = new JSONObject();
        if (login.size() != 0){
            jsonObject.put("code",1);
            jsonObject.put("msg","登陆成功");
            jsonObject.put("userMsg",login);

            // 登录成功保存session
            httpSession.setAttribute("name",name);

        }else {
            jsonObject.put("code",0);
            jsonObject.put("msg","登录失败");

        }
        return jsonObject;

    }
}
